<?php

namespace App\Models;

use App\Models\App;
use Log;
use Cache;

class VoiceSearchApp
{
    public function search($text, $matches)
    {
        if (isset($matches[2]) || preg_match('/^我(要|想)打开(\S+)/', $text, $matches)) {
            $key = $matches[2];
            $apps = App::where('name', $key)->get();
            if (!$apps) {
                return [
                    'status' => 1002,
                    'message' => '没有找到你想要的结果!',
                ];
            }
            $datas = [];
            foreach ($apps as $key => $app) {
                $datas[$key][0] = [
                    'model' => 'app',
                    'active_type' => 'mainActivity',
                    'name' => $app->name,
                    'verpic' => null,
                    'targetActions' => [
                        ['package_name' => $app->package_name]
                    ]
                ];
            }
            return [
                'status' => 0,
                'message' => '',
                'datas' => $datas
            ];
        } else {
            return [
                'status' => 1002,
                'message' => '没有找到你想要的结果!',
            ];
        }
    }
}
